<?php 

if(isset($argv)){
	$path = $argv[3];
	
}else{
	$path = $_GET['method'];
}

switch($path){
case "getpairs":
	echo public_query($path);
	break;
	
case "getinfo":
	if (isset($argv)) {
		$key = $argv[1]; // your API-key
		$secret = $argv[2]; // your Secret-key
		
	}
	else {
		$key = $_GET['public']; // your API-key
		$secret = $_GET['private']; // your Secret-key
		
	}
	echo private_query($path, $key, $secret);
	break;
	
case "tradeshistory":
	$req = array();
	
	$key = get_value_check_null("public");
	$secret = get_value_check_null("private");	
	
	$req['start_id']=get_value_check_null("start_id");
	$req['end_id']=get_value_check_null("end_id");
	$req['start_date']=get_value_check_null("start_date");
	$req['end_date']=get_value_check_null("end_date");
	$req['order']=get_value_check_null("order");
	$req['count']=get_value_check_null("count");
	$pair = get_value_check_null("pair");
	$req['pair']=$pair;

	if($_GET['My'] == 'True'){
		echo private_query($path, $key, $secret, $req);
		
	}else{
		$path .= '/';
		$path .= $pair;
		if(isset($_GET['time'])){
			$path .= '/';
			$path .= $_GET['time'];
		}
		echo public_query($path);
	}
	
	break;
	
case 'depth':
case 'ticker':
	if(isset($_GET['pair']))
		$pair = $_GET['pair'];

	$path .= '/';
	$path .= $pair;
	echo public_query($path);
		
	break;

case "transactions":
$req = array();

	$key = get_value_check_null("public");
	$secret = get_value_check_null("private");
	
	$req['start_id']=get_value_check_null("start_id");
	$req['end_id']=get_value_check_null("end_id");
	$req['start_date']=get_value_check_null("start_date");
	$req['end_date']=get_value_check_null("end_date");
	$req['order']=get_value_check_null("order");
	$req['count']=get_value_check_null("count");
	$req['currency']=get_value_check_null("currency");
	$req['type']=get_value_check_null("type");	
 
	echo private_query($path, $key, $secret, $req);	
	break;
	
case "ordershistory":
$req = array();

	$key = get_value_check_null("public");
	$secret = get_value_check_null("private");
	
	$req['start_id']=get_value_check_null("start_id");
	$req['end_id']=get_value_check_null("end_id");
	$req['start_date']=get_value_check_null("start_date");
	$req['end_date']=get_value_check_null("end_date");
	$req['order']=get_value_check_null("order");
	$req['count']=get_value_check_null("count");
	$req['pair']=get_value_check_null("pair");
	$req['show_active']=get_value_check_null("show_active");
	
	echo private_query($path, $key, $secret, $req);
	
	break;
	
case "trade":
$req = array();

	$key = get_value_check_null("public");
	$secret = get_value_check_null("private");
	
	$req['pair']=get_value_check_null("pair");
	$req['amount']=get_value_check_null("amount");
	$req['price']=get_value_check_null("price");
	$req['type']=get_value_check_null("type");	
	
	echo private_query($path, $key, $secret, $req);
	break;
	
case "orderinfo":
case "cancelorder":
$req = array();

	$key = get_value_check_null("public");
	$secret = get_value_check_null("private");
	$req['orderid']=get_value_check_null("orderid");
	
	echo private_query($path, $key, $secret, $req);
	break;
}

function public_query($path)
{ 
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/18.0 (compatible; CT-PHP client; '.php_uname('a').'; PHP/'.phpversion().')');
	curl_setopt($ch, CURLOPT_URL, 'https://crypto-trade.com/api/1/'.$path);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
	$res = curl_exec($ch);
	return $res;  
} 

function private_query($path, $key, $secret, array $req = array()) {
	$req['nonce'] = round(microtime(true) * 1000);
	$post_data = http_build_query($req, '', '&');
	$sign = hash_hmac('sha512', $post_data, $secret);
	$headers = array(
			'AuthKey: '.$key,
			'AuthSign: '.$sign,
	);
	static $ch = null;
	if (is_null($ch)) {
		$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_USERAGENT, 
	'Mozilla/18.0 (compatible; CT-PHP client; '.php_uname('a').'; PHP/'.phpversion().')'
	);}
	curl_setopt($ch, CURLOPT_URL, 'https://crypto-trade.com/api/1/private/'.$path);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	$res = curl_exec($ch);
	return $res;

}

function get_value_check_null($name){
	$val = NULL;
	if(isset($_GET[$name])){
		$val = $_GET[$name];
		if($val == "null")
			$val = NULL;
	}
	return $val;
}
//25815D21-AC46-BD4D-B3C4-3B510EB29696 3af30fa0bfb202dcd9360dd401167f0ad7678293 getinfo
?>
